dofile ("StylusBox2.lua")
dofile ("libColor.lua")

table.move = function (t, oldpos, newpos)
	local tmp = t[oldpos]
	table.remove(t, oldpos)
	table.insert(t, newpos, tmp)
end

table.moveOnTop = function (t, pos)
	local tmp = t[pos]
	table.insert(t, #t+1, tmp)
	table.remove(t, pos)
end

Window = {
	new = function (scr, x, y, w, h, title, text, c1, c2, c3, c4, options, text2, c5, c6, c7)
		local x2 = x+w
		local y2 = y+h
		local isMoving = false
		local isResizing = false
		local isMinimized = false
		local isMaximized = false
		local isClosed = false
		local hasFocus = false
		local titleBarH = 9
		local tmpx = 0
		local tmpy = 0
		local tmpw = 0
		local tmph = 0
		local ix = 0
		local iy = 0
		local tag = Window.all+1
		local id = Window.all+1
		local debugOn = false
		Window.all = Window.all+1
		
		if options == nil then
			options = {
				false,  -- StatusBar                               | 1
				true,  -- Maximize Button                     | 2
				true,  -- Minimize Button                      | 3
				true,  -- Close Button                            | 4
				true,  -- DoubleClicking on TitleBar      | 5
				true,  -- Resize box                                | 6
				true   -- Move action                              | 7
			}
		elseif type(options) == "boolean" then
			options = {
				options,  -- StatusBar                               | 1
				true,      -- Maximize Button                     | 2
				true,      -- Minimize Button                      | 3
				true,      -- Close Button                            | 4
				true,      -- DoubleClicking on TitleBar      | 5
				true,      -- Resize box                                | 6
				true       -- Move action                              | 7
			}
		end
		
		if c5 == nil then c5 = c1 end
		if c6 == nil then c6 = c4 end
		if c7 == nil then c7 = c2 end
		
		local draw = function (self)
			if id == #Window.windows then hasFocus = true else hasFocus = false end
			if not isClosed then
				if hasFocus then
					x2 = x+w
					y2 = y+h
					-- Moving --
					if options[7] then
						if StylusBox.simplePressed (x, y, x2-20, y+titleBarH, Stylus.X, Stylus.Y) then
							ix= Stylus.X-x
							iy = Stylus.Y-y
							isMoving = true
						end
						if Stylus.held and isMoving then
							x = Stylus.X-ix
							y = Stylus.Y-iy
						end
					end
					-- Resizing --
					if not isMinimized then
						if options[6] then
							if StylusBox.simpleHold (x2-5, y2-5, x2+5, y2+5, Stylus.X, Stylus.Y) then
								isResizing = true
							end
							if Stylus.held and isResizing then
								w = Stylus.X-x
								h = Stylus.Y-y
							end
						end
					end
					-- Maximizing --
					if options[2] then
						if StylusBox.simpleDoubleClicked (x, y, x2-20, y+titleBarH, Stylus.X, Stylus.Y) then
							if isMaximized then
								isMaximized = false
								x, y, w, h = tmpx, tmpy, tmpw, tmph
							else
								tmpx, tmpy, tmpw, tmph = x, y, w, h
								isMaximized = true
								isMinimized = false
							end
						end
						if isMaximized then
							x, y, w, h = 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT
						end
					end
					-- Minimizing --
					if options[3] then
						if StylusBox.simplePressed (x2-16, y+2, x2-10, y+8, Stylus.X, Stylus.Y) then
							if isMinimized then
								isMinimized = false
							else
								isMinimized = true
							end
						end
					end
					-- Closing --
					if options[4] then
						if StylusBox.simplePressed (x2-8, y+2, x2-2, y+8, Stylus.X, Stylus.Y) then
							table.remove(Window.windows, id)
							isClosed = true
						end
					end
					-- Useful tests --
					if x < 0 then x = 0 end
					if y < 0 then y = 0 end
					if x+10 > 246 then x = 246 end
					if y+9 > 192 then y = 192-9 end
					if w<60 then w = 60 end
					if h<40 then h = 40 end
					if Stylus.released then
						isMoving = false
						isResizing = false
					end
				else
					-- Focusing --
					local tx, ty, tw, th = Window.windows[#Window.windows]:getPositions()
					local tx2, ty2 = tx+tw, ty+th
					--Mls.gui:writeToConsole(tx.."-"..ty.."-"..x.."-"..y.."-"..Stylus.X.."-"..Stylus.Y)
					if StylusBox.simplePressed (x, y, x2, y2, Stylus.X, Stylus.Y)
						and not StylusBox.simplePressed (tx, ty, tx2, ty2, Stylus.X, Stylus.Y)
						and not hasFocus
					then
						hasFocus = true
						table.moveOnTop(Window.windows, id)
						id = #Window.windows
					end
				end
				for k, v in pairs(Window.windows) do
					if Window.windows[k]:getTag() == tag then
						id = k
					end
					--screen.print(SCREEN_UP, 5, 30, tostring(tag))
				end
				-- Drawing --
				screen.drawGradientRect(scr, x, y, x2, y+9, c1, c1, c1, c4)
				screen.drawTextBox (scr, x+1, y+1, x2-1, y+9, title, c2)
				if options[3] then
					screen.drawFillRect(scr, x2-15, y+2, x2-10, y+7, c_blue)
					screen.drawRect(scr, x2-16, y+2, x2-10, y+8, c_black)
				end
				if options[4] then
					screen.drawFillRect(scr, x2-7, y+2, x2-2, y+7, c_red)
					screen.drawRect(scr, x2-8, y+2, x2-2, y+8, c_black)
				end
				if not isMinimized then
					screen.drawGradientRect(scr, x, y+9, x2, y2, c3, c3, c3, c3)
					screen.drawTextBox (scr, x+1, y+11,x2-1, y2-1, text, c2)
					screen.drawRect (scr, x, y, x2, y2, c2)
					screen.drawLine (scr, x, y+9, x2, y+9, c2)
					if options[1] then
						screen.drawGradientRect(scr, x, y2-10, x2, y2, c5, c5, c5, c6)
						screen.drawRect(scr, x, y2-11, x2, y2+1, c_black)
						screen.drawTextBox(scr, x+2, y2-9, x2-3, y2-2, text2, c7)
					end
				else
					screen.drawRect (scr, x, y, x2, y+9, c2)
				end
				if not isMaximized then
					if not isMinimized then
						if options[6] then
							screen.drawLine (scr, x2-6, y2-3, x2-2, y2-3, c2)
							screen.drawLine (scr, x2-3, y2-6, x2-3, y2-2, c2)
						end
					end
				end
				--[[ !!! DeBuG !!! ]]--
				if debugOn then
					screen.print (SCREEN_UP, 5, (id-1)*11, tostring(id).." * ")
					--Mls.gui:writeToConsole (id)
					for k, v in pairs(Window.windows) do
						screen.print (SCREEN_UP, 30, (k-1)*11, tostring(k).." - "..tostring(v).." / "..tostring(Window.windows[k]:getID()).." & "..tostring(Window.windows[k]:getTag()).." .")
					end
				end
			end
		end
		
		local getID = function (self)
			return id
		end
		
		local getTag = function (self)
			return tag
		end
		
		local getFocus = function (self)
			return hasFocus
		end
		
		local getPositions = function (self)
			return x, y, w, h
		end
		
		local getTitle = function (self)
			return title
		end
		
		local getText = function (self)
			return text
		end
		
		local hasStatusBar = function (self)
			return options[1]
		end
		
		local getStatusText = function (self)
			return text2
		end
		
		local getMRMMC = function (self)
			return isMoving, isResizing, isMaximized, isMinimized, isClosed
		end
		
		local setFocus = function (self)
			hasFocus = true
			table.moveOnTop(Window.windows, id)
			id = #Window.windows
		end
		
		local setDebug = function (self, newdebug)
			debugOn = newdebug
		end
		
		local setPositions = function (self, nx, ny, nw, nh)
			x, y, w, h = nx, ny, nw, nh
		end
		
		local setTitleBarHeight = function (self, newH)
			titleBarH = newH
		end
		
		local setTitle = function (self, ntitle)
			title = ntitle
		end
		
		local setText = function (self, ntext)
			text = ntext
		end
		
		local addStatusBar = function (self, text)
			options[1] = true
			text2 = text
		end
		
		local setStatusText = function (self, text)
			text2 = text
		end
		
		local setMMC = function (m1, m2, c)
			isMaximized, isMinimized, isClosed = m1, m2, c
		end
		
		local clear = function (self)
			self = nil
		end
    
   return{
		-- draw --
		draw = draw,
		-- get --
		getID = getID,
		getTag = getTag,
		getFocus = getFocus,
		getPositions = getPositions,
		getTitle = getTitle,
		getText = getText,
		hasStatusBar = hasStatusBar,
		getStatusText = getStatusText,
		getMRMMC = getMRMMC,
		-- set --
		setFocus = setFocus,
		setDebug = setDebug,
		setPositions = setPositions,
		setTitleBarHeight = setTitleBarHeight,
		setTitle = setTitle,
		setText = setText,
		addStatusBar = addStatusBar,
		setStatusText = setStatusText,
		setMMC = setMMC,
		-- clear --
		clear = clear
   }
	end,
	
	add = function (window)
		table.insert(Window.windows, window:getID(), window)
	end,
	
	drawAll = function ()
		for i = 1, #Window.windows do
			Window.windows[i]:draw()
		end
	end
}
Window.all = 0
Window.windows = {}
